<?php

namespace app\data\controller;

use app\data\controller\base\Publics;
use think\Db;
use think\Log;

/**
 * 终端绑定
 * Class
 * @package app\data\controller
 */
class Binds
{

	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {

			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 绑定信息存储
	 */
	public function customer_active($sn)
	{
		$day = config("nums.actDay");
		$binds = Publics::instance()->snBinds($sn);
		if ($binds["bindTime"]) {
			$binds["bindTime"] = strtotime(date("Y-m-d", $binds["bindTime"]));
			$endTimes = strtotime("-1 day", strtotime("+" .  $day . " day", $binds["bindTime"]));
			$option = [
				"uid" => $binds["uid"],
				"bind_times" => $binds["bindTime"],
				"end_times" => $endTimes,
				"money" => "0.00",
				"proid" => $binds["proid"],
				"sn" => $binds["sn"],
				"termDays" => $day,
				"create_time" => time()
			];
			$checkStore = Publics::instance()->checkBindStore($option["sn"]);
			if ($checkStore) {
				$money = Trans::instance()->sn_active_before_trans(Publics::instance()->getDataTable($option["proid"]), $option["sn"],  $option['bind_times'], $option['end_times']);
				$option["money"] = $money;
				$option["stands_money"] =  config("nums.standsNo");
				if (intval($money) >= intval($option["stands_money"])) {
					$option['stands_typs'] = 2;
				}
				Db::name(Publics::instance()->tab()->acte_stands)->insert($option);
			}
		}
	}


	/**
	 * 流失客户信息存储
	 */
	public function customer_lost($uid)
	{
		try {
			$day = config("nums.transDay");
			$checkStore = Publics::instance()->checkLostStore($uid);
			if($checkStore){
				$option = [
					"uid" => $uid,
					"termDays" => $day,
					"last_time" => time(),
					"create_time" => time()
				];
				Db::name(Publics::instance()->tab()->lost)->insert($option);
			}
		} catch (\Exception $e) {
			Log::write($e->getMessage());
		}
	}
}
